package com.vrsys.controller;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ServletContextAware;

import com.vrsys.task.ScheduleTask;
import com.vrsys.task.service.IDpreCheckService;
import com.vrsys.util.IDGenerator;

public class IDTaskController extends BaseController implements ServletContextAware{
    // servletContext will be injected automatically after IDTaskController was initialized, no @Autowired annotation needed.
    private ServletContext servletContext;
    @Autowired
    private IDGenerator idGenerator;
    @Autowired
    private ScheduleTask scheduleTask;
    private String idStorePropertiesFile;

    @Autowired
    private IDpreCheckService idPreCheckService;

    public IDTaskController() {}

    public IDTaskController(String idStorePropertiesFile) {
        this.idStorePropertiesFile = idStorePropertiesFile;
    }


    public void scheduleTask() throws Exception {
        logger.info(servletContext.getRealPath(idStorePropertiesFile));
        idPreCheckService.validateIds(servletContext.getRealPath(idStorePropertiesFile));
        scheduleTask.getIdSaveTask().setIdGenerator(idGenerator);
        scheduleTask.scheduleTask();
    }

    public void destroyTask() {
        try {
            scheduleTask.destroyTask();
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    /*
     * will inject servletContext after the controller was initialized
     * (non-Javadoc)
     * @see org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet.ServletContext)
     */
    @Override
    public void setServletContext(ServletContext servletContext) {
//        logger.info("setting context...");
        this.servletContext = servletContext;
    }
}
